package ipdesk.model.report.design.dao;

import java.util.List;
import java.util.Map;

import ipdesk.model.report.brand.bean.BrandInfoBean;
import ipdesk.model.report.design.bean.DesignInfoBean;
import ipdesk.model.report.promote.bean.PromoteInfoBean;

import com.ibatis.dao.client.DaoManager;
import com.opensymphony.xwork2.ActionSupport;

import ecg.dao.DaoService;
import ecg.util.FileService;

public class DesignInfoDao extends ActionSupport
{
	private static DesignInfoDao singleton;
	
	protected DesignInfoDao(){}
	
	public static DesignInfoDao getInstance()
	{
		if(singleton == null)
		{
			singleton = new DesignInfoDao();
		}
		
		return singleton;
	}
	
	private DaoManager	manager	= DaoService.getInstance();
	private IDesignInfo designInfo 	= (IDesignInfo)manager.getDao(IDesignInfo.class, "ip_desk");
	
	
	/*
	 * 입력받은 데이터를 DB에 insert 하는 메서드.
	 */
	public int designInfoReg(DesignInfoBean designInfoBean) throws Exception
	{
		int lastDesignSeq = 0;
		
		try{
			// 트랜잭션 시작
			manager.startTransaction();
			
			
			
			// 상담정보 insert.
			lastDesignSeq = designInfo.designInfoReg(designInfoBean);
		
			// 트랜잭션 커밋.
			manager.commitTransaction();
		}finally{
			// 트랜잭션 끝.
			manager.endTransaction();
		}
					
	
		return lastDesignSeq;
	}
	
	public void insertFileData(DesignInfoBean designInfoBean) throws Exception
	{
		try{
			manager.startTransaction();
			
			designInfo.insertFileData(designInfoBean);
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}				
		
	}
	
	public int getDataTotalCount(DesignInfoBean designInfoBean) throws Exception
	{
		
		int totalDataCount = designInfo.getDataTotalCount(designInfoBean); 
		
		return totalDataCount;
	}
	
	public List<DesignInfoBean> getPagingDataListForUser(Map map) throws Exception
	{
		
		List<DesignInfoBean> pageDataList = designInfo.getPagingDataListForUser(map);
		

		return pageDataList; 
	}
	
	public DesignInfoBean getDetailData(int designSeq) throws Exception
	{
		DesignInfoBean designInfoBean = designInfo.getDetailData(designSeq);
		
		designInfoBean.setEmblem(designInfoBean.getEmblem().replace("\n", "<br/>"));
		//designInfoBean.setSimilarDesign(designInfoBean.getSimilarDesign().replace("\n", "<br/>"));
		//designInfoBean.setComment(designInfoBean.getComment().replace("\n", "<br/>"));
		
		return designInfoBean;
	}
	
	public DesignInfoBean getModifyData(int designSeq) throws Exception
	{
		DesignInfoBean designInfoBean = designInfo.getDetailData(designSeq);
		
		designInfoBean.setEmblem(designInfoBean.getEmblem());
		//designInfoBean.setSimilarDesign(designInfoBean.getSimilarDesign());
		//designInfoBean.setComment(designInfoBean.getComment());
		
		return designInfoBean;
	}
	
	/**
	 * 파일 삭제
	 */
	public boolean fileDelete(int designSeq)
	{
		boolean deleteFlag = false;
		
		// designSeq에 해당하는 파일 데이터를 가지고 온다.
		DesignInfoBean designInfoBean = designInfo.getDataOneRow(designSeq, "design.designUploadData");
		
		// 데이터가 존재한다면 데이터 삭제. 파일 삭제
		if(designInfoBean != null)
		{
			try{
				manager.startTransaction();
				// 파일 데이터 삭제
				designInfo.deleteDataOneRow(designSeq, "design.deleteUploadData");
				manager.commitTransaction();
				
				String filePathPrefix = getText("path.upload.prefix");
				String filePath = designInfoBean.getPathName() + designInfoBean.getUploadFileName();
				// 파일 삭제
				FileService fs = new FileService();
				fs.deleteFile(filePathPrefix + filePath);
				
				deleteFlag = true;
			}finally{
				manager.endTransaction();
			}
		}
		
		return deleteFlag;
	}
	
	
	/**
	 *  해당 promoteSeq에 대한 데이터를 업데이트 한다.
	 */
	public int designInfoUpdate(DesignInfoBean designInfoBean) throws Exception
	{
		
		int result = 0;
		
		try{
			manager.startTransaction();
			
			// 상표출원 상담일지 데이터 업데이트.
			result = designInfo.designInfoUpdate(designInfoBean, "design.updateDesignData");
			
			manager.commitTransaction();
			
		}finally{
			manager.endTransaction();
		}
		
		
		return result;
	}
	
	/**
	 * 데이터 삭제
	 * 
	 */
	public boolean designInfoDelete(int designSeq)
	{
		
		boolean fileDeleteFlag = false; 
		boolean deleteFlag = false;
		
		try{
			manager.startTransaction();
			
			// design 데이터 삭제.
			designInfo.deleteDataOneRow(designSeq, "design.deleteDesignData");
				
			deleteFlag = true;
			
			manager.commitTransaction();
		}finally{
			manager.endTransaction();
		}
		
		return deleteFlag;
	}
	
	/**
	 *  파일 상세보기
	 * @param designSeq
	 * @param fileCategory
	 * @return
	 * @throws Exception
	 */
	public DesignInfoBean getFileDetailData(int designSeq, String fileCategory) throws Exception
	{
		DesignInfoBean designInfoBean = new DesignInfoBean();
		designInfoBean.setDesignSeq(designSeq);
		designInfoBean.setFile_category(fileCategory);
		
		return designInfo.getFileDetailData(designInfoBean);
	}
	
	/**
	 * 완료보고서 등록
	 * @param designInfoBean
	 * @throws Exception
	 */
	public void insertCompleteData(DesignInfoBean designInfoBean) throws Exception
	{
		designInfo.insertCompleteData(designInfoBean);
	}
	
	/**
	 * 파일 삭제하기
	 * @param fileSeq
	 * @throws Exception
	 */
	public void delFileData(String fileSeq) throws Exception
	{
		designInfo.delFileData(fileSeq);
	}
	
}
